package components
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.system.Security;
	
	import mx.controls.HTML;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.events.ResizeEvent;
	
	import nl.wv.extenders.panel.SuperPanel;
	
	public class loadHTMLPage extends Sprite
	{
		public var panel:SuperPanel;
		public var webPage:HTML;
		public var btn:Image;
		public var refresh:Image;
		public var goBack:Image;
		public var goForward:Image;
		public var webLink:Label;
		//public var history:HTMLHistoryItem;
		public var backVisible:Boolean;
		public var forwardVisible:Boolean;
		
		public function loadHTMLPage(p:SuperPanel)
		{
			panel = new SuperPanel();
			panel = p;
		}
		
		public function loadHTMLTekst(tekst:String):void{
			webPage = new HTML();
			webPage.data = tekst;
	 		webPage.x=10;
	 		webPage.y=34;
	 		webPage.height=panel.height-75;
	 		webPage.width=panel.width-35;
	 		panel.addEventListener(ResizeEvent.RESIZE, onResize1);
		}
		
		public function loadHTML(link:String):void{
			webPage = new HTML();
			btn = new Image();
			webLink = new Label();
			refresh = new Image();
			goBack = new Image();
			goForward = new Image();
			backVisible=false;
			forwardVisible=false;
			btn.source = "./icons/go.png";
			refresh.source = "./icons/refresh.png";
			webLink.x=10;
			webLink.y=panel.height-42;
			webLink.width=panel.width-45;
			webLink.height=30;
			webLink.selectable = true;
			webPage.location=link;
	 		webPage.x=10;
	 		webPage.y=34;
	 		webPage.height=panel.height-75;
	 		webPage.width=panel.width-35;
			btn.y = panel.height-39;
			btn.x =panel.width-45;
			btn.buttonMode = true;
			btn.useHandCursor = true;
			btn.toolTip="Ucitaj stranicu";
			goBack.y = panel.height-39;
			goBack.x =panel.width-85;
			goBack.buttonMode = true;
			goBack.useHandCursor = true;
			goBack.toolTip="Idi nazad";
			goBack.source = "./icons/back.png";
			goForward.y = panel.height-39;
			goForward.x =panel.width-65;
			goForward.buttonMode = true;
			goForward.useHandCursor = true;
			goForward.toolTip="Idi naprijed";
			goForward.source = "./icons/forward.png";
			refresh.y = panel.height-39;
			refresh.x =panel.width-45;
			refresh.buttonMode = true;
			refresh.useHandCursor = true;
			refresh.toolTip="Osvjezi stranicu";
			panel.addChild(webLink);
			panel.addChild(btn);
	 		webPage.addEventListener(Event.COMPLETE, webPageLoaded);
	 		panel.addEventListener(ResizeEvent.RESIZE, onResize);
	 		btn.addEventListener(MouseEvent.CLICK, onLoad);
	 	}
	 	
	 	public function onLoad(e:MouseEvent):void{
	 		panel.addChild(webPage);
	 		panel.removeChild(btn);
	 		panel.addChild(refresh);
	 		refresh.addEventListener(MouseEvent.CLICK, onRefresh);
	 	}
	 	
	 	public function onRefresh(e:Event):void{
	 		webPage.reload();
	 	}
	 	
	 	public function onResize(e:Event):void{
	 		webPage.height=panel.height-75;
	 		webPage.width=panel.width-35;
			btn.y = panel.height-39;
			btn.x =panel.width-45;
			goBack.y = panel.height-39;
			goBack.x =panel.width-85;
			goForward.y = panel.height-39;
			goForward.x =panel.width-65;
			refresh.y = panel.height-39;
			refresh.x =panel.width-45;
			webLink.y=panel.height-42;
			webLink.width=panel.width-45;
	 	}
	 	
	 	public function onResize1(e:Event):void{
	 		webPage.height=panel.height-75;
	 		webPage.width=panel.width-35;
	 	}
	 	
	 	public function webPageLoaded(e:Event):void{
	 		webLink.htmlText = "Naslov: "+"<b>"+webPage.htmlLoader.getHistoryAt(webPage.htmlLoader.historyPosition).title+"</b>\nURL: "+"<b>"+webPage.htmlLoader.getHistoryAt(webPage.htmlLoader.historyPosition).url+"</b>";
	 		if(webPage.htmlLoader.historyPosition>0){
	 			if(!backVisible){
	 				panel.addChild(goBack);
	 				goBack.addEventListener(MouseEvent.CLICK, goBackClicked);
	 				backVisible=true;
	 			}
	 			if(!forwardVisible){
	 				panel.addChild(goForward);
	 				goForward.addEventListener(MouseEvent.CLICK, goForwardClicked);
	 				forwardVisible=true;
	 			}		
	 		}
	 			if(webPage.htmlLoader.historyPosition==0&&backVisible){
	 				panel.removeChild(goBack);
	 				goBack.removeEventListener(MouseEvent.CLICK, goBackClicked);
	 				backVisible=false;	 				
	 			}
	 			if(webPage.htmlLoader.historyPosition==webPage.htmlLoader.historyLength-1&&forwardVisible){
	 				panel.removeChild(goForward);
	 				goForward.removeEventListener(MouseEvent.CLICK, goForwardClicked);
	 				forwardVisible=false;	 				
	 			}	 
	 	}
	 	
	 	public function goBackClicked(e:Event):void{
	 		webPage.htmlLoader.historyBack();
	 	}
	 	
	 	public function goForwardClicked(e:Event):void{
	 		webPage.htmlLoader.historyForward();
	 	}
	 	
	 	public function load():void{
	 		panel.addChild(webPage);
	 		panel.removeChild(btn);
	 		panel.addChild(refresh);
	 		refresh.addEventListener(MouseEvent.CLICK, onRefresh);
	 	}
	 	
	 	public function loadTekst():void{
	 		panel.addChild(webPage);
	 	}
	}
}